package cn.commander.controller;

import cn.commander.base.BaseController;
import cn.commander.model.CusPageInfo;
import cn.commander.model.User;
import cn.commander.service.UserService;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/users")
public class UserController extends BaseController {

	private Logger logger = Logger.getLogger(UserController.class);

	@Autowired
	private UserService userService;

	@ApiOperation(value = "获取所有用户", notes = "无参数")
	@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
	public Object getUserInfo(@ModelAttribute CusPageInfo cusPageInfo) {
		return userService.getUserInfo(cusPageInfo);
	}

	@ApiOperation(value = "根据名称获取用户", notes = "参数：name")
	@RequestMapping(value = "/getUserInfoByName", method = RequestMethod.GET)
	public Object getUserInfoByName(@RequestParam(value = "name") String name) {
		logger.info("根据名称获取用户");
		return userService.getUserByName(name);
	}

	@ApiOperation(value = "根据主键获取用户", notes = "参数：name")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Object selectByPrimaryKey(@PathVariable(value = "id") String id) {
		logger.info("根据主键获取用户");
		return userService.selectByPrimaryKey(id);
	}

	@ApiOperation(value = "根据名称更新用户信息", notes = "参数：name")
	@RequestMapping(value = "/updateUserInfoByName", method = RequestMethod.GET)
	public Object updateUserInfoByName(User user) {
		return userService.updateByName(user);
	}

	@ApiOperation(value = "新增用户信息", notes = "参数：name,age")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public Object insert(@ModelAttribute User user) {
		return userService.insert(user);
	}
}
